package oboe.controllers.jml.helpers;

import com.adobe.serialization.json.JSON;

import flash.display.Bitmap;
import flash.display.DisplayObject;
import flash.display.Loader;
import flash.display.Sprite;
import flash.events.Event;
import flash.net.URLLoader;
import flash.net.URLRequest;

import oboe.controllers.jml.events.JMLEvent;
import oboe.utils.Prototype;

import flash.Vector;

class JMLSlideshow extends Sprite
{
public var stream:String;
private var loader:URLLoader;
public var src:Array;
private var imgs:Vector<Loader>;
public var currentImage:Int;

public var fadeOutImage:Loader;
public var maxImageTime:Float = 5;
private var currentImageDisplayTime:Float;
public var autoPlay:Bool = true;

public var maxSize:Float

public function new()
{
	super();
	this.addEventListener( JMLEvent.JMLCOMPLETE, onComplete);
	this.currentImage = -1;
	this.currentImageDisplayTime = 0;
}

private function onComplete( e:JMLEvent ):Void
{
	if( stream )
	{
	this.loader = new URLLoader();
	this.loader.addEventListener(Event.COMPLETE, onGotStream );
	this.loader.load( new URLRequest( stream ) );
	}
	else if( src )
	{
	this.loadImages();
	}
	Object(this).addCallback( Event.ENTER_FRAME, onTick, 0.013 );
}

private function onGotStream( e:Event ):Void
{
	this.src = JSON.decode( this.loader.data )[0] as Array<Dynamic>;
	this.loadImages();
}

private function loadImages():Void
{
	imgs = new Vector<Loader>( this.src.length, true );
	this.currentImageDisplayTime = this.maxImageTime;
}

public function onTick( time:Float ):Void
{
	if( this.fadeOutImage && this.fadeOutImage.parent == this )
	{
	var alpha:Float = this.fadeOutImage.alpha - time;
	if( alpha <= 0 )
	{
		alpha = 0;
		this.removeChild( this.fadeOutImage );
		this.fadeOutImage.alpha = 1;
		this.fadeOutImage = null;
	}
	else
	{
		this.fadeOutImage.alpha = alpha;
	}
	this.imgs[ this.currentImage ].alpha = 1-alpha;			
	}
	
	if( !imgs || !imgs.length ) return;
	
	this.currentImageDisplayTime += time;
	if( this.currentImageDisplayTime > this.maxImageTime )
	{
	if( this.currentImage > -1 )
	{
		if( this.fadeOutImage && this.fadeOutImage.parent == this )
		{
		this.removeChild( this.fadeOutImage );
		this.fadeOutImage.alpha = 1;
		}
		this.fadeOutImage = this.imgs[ this.currentImage ];
	}
	this.currentImage = (this.currentImage+1)%imgs.length;
	
	if( !this.imgs[ this.currentImage ] )
	{
		var ldr:Loader = this.imgs[ this.currentImage ] = new Loader();
		Prototype.addCallbackEx( ldr.contentLoaderInfo, Event.COMPLETE, onLoadingComplete, this.currentImage ); 
		Object(ldr).loadEx( new URLRequest( src[ this.currentImage ] ) );
		this.currentImageDisplayTime = -1;
	}
	else if( this.imgs[ this.currentImage ].contentLoaderInfo.content )
	{
		this.onLoadingComplete( null, this.currentImage );
		this.currentImageDisplayTime = 0;
	}
	}
}

private function onLoadingComplete( e:Event, id:Int ):Void
{
	if( id != this.currentImage ) return;
	Bitmap( this.imgs[ id ].content ).smoothing = true;
	var img:DisplayObject = this.imgs[ id ];
	var max:Int = Math.max( img.width, img.height );
	img.scaleX = img.scaleY *= maxSize/max;
	img.x = maxSize/2 - img.width/2;
	img.y = maxSize/2 - img.height/2;
	this.addChild( img );
	if( this.fadeOutImage )	img.alpha = 0;
}
}